package com.smh.demo1.a02;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletRegistrationBean;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

/**
 * @author shiminghui
 * @date 2025/4/10 15:54
 * @description: TODO
 */
public class TestApplicationContext {
    public static void main(String[] args) {
//        test1();
//        test2();
//        test3();
//        test4();
        test5();
    }


    public static void test1() {
        // 通过xml配置文件来创建bean
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("testXml01.xml");
        for (String name : context.getBeanDefinitionNames()) {
            System.out.println(name);
        }
        // 实际内部就是有一个DefaultListableBeanFactory,然后通过XMLReader来解析xml文件,然后调用DefaultListableBeanFactory的registerBeanDefinition方法来注册bean

    }

    public static void test2() {
        FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("D:\\develop\\Code\\JavaCode\\springboot-study\\demo1\\src\\main\\resources\\testXml01.xml");
        for (String name : context.getBeanDefinitionNames()) {
            System.out.println(name);
        }
    }

    public static void test3() {
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        for (String beanDefinitionName : beanFactory.getBeanDefinitionNames()) {
            System.out.println(beanDefinitionName);
        }
        System.out.println("===========================================================================");
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
        reader.loadBeanDefinitions("testXml01.xml");

        for (String beanDefinitionName : beanFactory.getBeanDefinitionNames()) {
            System.out.println(beanDefinitionName);
        }

    }

    // 基于java配置类实现
    public static void test4() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
        for (String beanDefinitionName : context.getBeanDefinitionNames()) {
            System.out.println(beanDefinitionName);
        }
        Bean2 bean = context.getBean(Bean2.class);
        System.out.println(bean.getBean1());
    }

    // 测试基于java配置类实现web环境
    public static void test5() {
        AnnotationConfigServletWebServerApplicationContext context =
                new AnnotationConfigServletWebServerApplicationContext(WebConfig.class);

    }

    //    @Configuration
    static class WebConfig {
        // 创建一个ServletWebServerFactory,这里是一个tomcat的实现
        @Bean
        public ServletWebServerFactory servletWebServerFactory() {
            TomcatServletWebServerFactory tomcatServletWebServerFactory = new TomcatServletWebServerFactory();
            tomcatServletWebServerFactory.setPort(8000);
            tomcatServletWebServerFactory.setContextPath("");
            return tomcatServletWebServerFactory;
        }

        // 创建一个DispatcherServlet,这是web的前处理器
        @Bean
        public DispatcherServlet dispatcherServlet() {
            DispatcherServlet dispatcherServlet = new DispatcherServlet();
            return dispatcherServlet;
        }

        // 将web的DispatcherServlet注册到容器中
        @Bean
        public DispatcherServletRegistrationBean dispatcherServletRegistrationBean(DispatcherServlet dispatcherServlet) {
            DispatcherServletRegistrationBean registrationBean = new DispatcherServletRegistrationBean(dispatcherServlet, "/"); // 设置DispatcherServlet的映射路径
            registrationBean.setLoadOnStartup(1);
            return registrationBean;
        }

        @Bean("/hello") // 设置映射路径
        public Controller controller1() {
            return (request, response) -> {
                response.getWriter().print("hello");
                return new ModelAndView();
            };
        }

    }

    //    @Configuration
    static class Config {
        @Bean
        public Bean1 bean1() {
            return new Bean1();
        }

        @Bean
        public Bean2 bean2(Bean1 bean1) {
            Bean2 bean2 = new Bean2();
            bean2.setBean1(bean1);
            return bean2;
        }
    }

    static class Bean1 {
        private final static Logger log = LoggerFactory.getLogger(Bean1.class);

        public Bean1() {
            log.info("Bean1 init");
        }
    }

    static class Bean2 {
        private Bean1 bean1;

        public Bean1 getBean1() {
            return bean1;
        }

        public void setBean1(Bean1 bean1) {
            this.bean1 = bean1;
        }
    }
}
